﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Entity.Business;
using YiSha.Entity.OrganizationManage;
using YiSha.Model.Param.Business;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Service.OrganizationManage;
using YiSha.Util.Extension;
using YiSha.Util.Model;

namespace YiSha.Business.Business
{
    public class EyeFollowUpBLL
    {
        private EyeFollowUpService eyeFollowUpService = new EyeFollowUpService();
       
        #region 获取数据
        public async Task<TData<List<EyeFollowUpEntity>>> GetList(EyeFollowUpParam param)
        {
            TData<List<EyeFollowUpEntity>> obj = new TData<List<EyeFollowUpEntity>>();
            obj.Data = await eyeFollowUpService.GetList(param);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<EyeFollowUpEntity>>> GetPageList(EyeFollowUpParam param, Pagination pagination)
        {
            TData<List<EyeFollowUpEntity>> obj = new TData<List<EyeFollowUpEntity>>();
            obj.Data = await eyeFollowUpService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<EyeFollowUpEntity>> GetEntity(long id)
        {
            TData<EyeFollowUpEntity> obj = new TData<EyeFollowUpEntity>();
            obj.Data = await eyeFollowUpService.GetEntity(id);
            obj.Tag = 1;
            return obj;
        }


        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(EyeFollowUpEntity entity)
        {
            TData<string> obj = new TData<string>();

            if (entity.ThisDate == default||entity.NextFollowUpTime==default)
            {
                obj.Message = "请录入有效的随访日期和下次随访日期！";
                return obj;
            }

            if (eyeFollowUpService.ExistName(entity))
            {
                obj.Message = "今天已经录入过体检！";
                return obj;
            }
            if (entity.FollowUpSort == 0)
            {
                entity.FollowUpSort = GetMaxSort(entity.PrescriptionId).Result;
            }
            await eyeFollowUpService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }
        /// <summary>
        /// 根据处方Id获取次数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> GetMaxSort(long id)
        {
           return await eyeFollowUpService.GetMaxSort(id);
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await eyeFollowUpService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion
    }
}
